package com.didispace.service;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.command.AsyncResult;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class ComputeService {

    @Autowired
    RestTemplate restTemplate;

    @HystrixCommand(fallbackMethod = "addServiceFallback")
    public String addService() {
    	System.out.println(Thread.currentThread().getName());
        return restTemplate.getForEntity("http://COMPUTE-SERVICE/add?a=10&b=20", String.class).getBody();
    }
    
    @HystrixCommand(fallbackMethod = "addServiceFallback")
    public Future<String> addService1() {
    	System.out.println(Thread.currentThread().getName());
    	AsyncResult<String> future = new AsyncResult<String>() {

			@Override
			public String invoke() {
				// TODO Auto-generated method stub
				return restTemplate.getForEntity("http://COMPUTE-SERVICE/add?a=10&b=20", String.class).getBody();
			}
		};
    	return future;
    }

    public String addServiceFallback() {
        return "error";
    }
    /*public AsyncResult<String> addServiceFallback1() {
    	return new AsyncResult<String>() {

			@Override
			public String invoke() {
				// TODO Auto-generated method stub
				return "asyn error";
			}};
    }*/

}
